$(function () {
    var user = null,
        thisPage = null,
        applyPage = function () {
            this.init();
        };

    applyPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            //判断是否登录
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user) {
                redirect("../account/login.html");
                return false;
            }
            else {
                $.ajax({
                    url: CONFIG_MANAGER.RequstUrl + "v1/member/" + user.id,
                    type: "GET",
                    async: true,
                    headers: {"token": user.token},
                    beforeSend: function () {
                        $.showWaiting();
                    }
                }).done(function (data) {
                    //若未签约
                  data.is_agent = false;
                    if (!data.is_agent) {
                        $("#is_agent_sign").show();

                        $("#sign").on("tap", function () {
                            if (!$("#checkbox-1-1_aggree").is(":checked")) {
                                $.alert("请先阅读协议并选中确认");
                                return false;
                            }
                            $.ajax({
                                type: "POST",
                                url: CONFIG_MANAGER.RequstUrl + "v1/member/sign/agent/" + user.id,
                                async: true,
                                headers: {token: user.token},
                                beforeSend: function () { $.toast("等待..."); }
                            }).done(function (data) {
                                $.alert("签约成功");
                            }).fail(function (data) {
                                var infor = JSON.parse(data);
                                $.alert(infor.message);
                            });
                        });
                    }
                }).always(function (data) {
                    $.closeWaiting();
                });
            }
        },

        bindEvent: function () {
            //返回
            $("#back").on("click", function () {
                back();
            });

            //
            $("#merchantSelect").on("tap", function () {
                redirect("select.html");
                return false;
            });
        },
    };
    var thisPage = new applyPage();
});
